package ua.com.alus.resvac.crm.lucene;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.LowerCaseFilter;
import org.apache.lucene.analysis.StopAnalyzer;
import org.apache.lucene.analysis.StopFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardFilter;
import org.apache.lucene.analysis.standard.StandardTokenizer;
import org.apache.lucene.util.Version;

import java.io.Reader;

/**
 * Synonym analyzer
 *
 * @author Usatov Alexey
 */
public class SynonymAnalyzer extends Analyzer {
    private SynonymEngine engine;

    public SynonymAnalyzer(SynonymEngine engine) {
        this.engine = engine;
    }

    public TokenStream tokenStream(String fieldName, Reader reader) {
        return new SynonymFilter(
                new StopFilter(Version.LUCENE_36, new LowerCaseFilter(Version.LUCENE_36,
                        new StandardFilter(Version.LUCENE_36, new StandardTokenizer(Version.LUCENE_30, reader))),
                        StopAnalyzer.ENGLISH_STOP_WORDS_SET), engine);
    }
}
